(define (double x) (+ x x))

(define (halve x) (/ x 2))

(define (even? n)
   (= (remainder n 2) 0))

(define (mul a b)
   (cond ((< b 0)(- (mul a (- b))))
         ((= b 0) 0)
         ((= b 1) a)
         ((even? b) (double (mul a (halve b))))
         (else (+ a (mul a (- b 1))))))

(define (mul-i a b)
  (if (> b 0) (mul-iter a b 0)
      (- (mul-iter a (- b) 0))))

; the invariant here is that result = a*b + c = result
(define (mul-iter a b c)
  (cond ((= b 0) c)
        ((even? b) (mul-iter (double a) (halve b) c))
        (else (mul-iter a (- b 1) (+ c a)))))